﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Sec
{
    public class SecMenuBs
    {
        public static List<SecMenuEntity> GetAllMenus()
        {
            string sql = "SELECT * FROM SEC_MENU WHERE METHOD IS NULL ORDER BY ORDER_INDEX ";
            List<SecMenuEntity> list = DataAccess.Select<SecMenuEntity>(sql);
            return list;
        }

        public static DataTable GetAllDsMenus()
        {
            string sql = @"SELECT sm.MENU_ID as Id,sm.PARENT_MENU as ParentId,sm.MENU_TYPE_CODE as ObjectType,sm.MENU_NAME as Name
FROM SEC_MENU sm WHERE METHOD IS NULL ORDER BY ORDER_INDEX ";
            DataSet ds = DataAccess.SelectDataSet(sql);
            return ds.Tables[0];
        }

        public static List<SecMenuEntity> GetAllFormMenus()
        {
            string sql = @"SELECT M.* FROM SEC_MENU M
                            INNER JOIN SEC_MENU_TYPE MT ON
	                            MT.MENU_TYPE_CODE = M.MENU_TYPE_CODE
                            WHERE MT.MENU_TYPE_NAME = 'FORM' AND M.METHOD IS NULL ORDER BY ORDER_INDEX ";
            List<SecMenuEntity> list = DataAccess.Select<SecMenuEntity>(sql);
            return list;
        }

        public static DataSet GetDSAllMenus()
        {
            string sql = "SELECT * FROM SEC_MENU WHERE METHOD IS NULL ORDER BY ORDER_INDEX ";
            DataSet ds = DataAccess.SelectDataSet(sql);
            return ds;
        }

        public static List<SecMenuEntity> GetMenusByParentID(string parentMenuID)
        {
            if (string.IsNullOrEmpty(parentMenuID))
            {
                return GetRootMenus();
            }
            string sql = "SELECT * FROM SEC_MENU WHERE PARENT_MENU = '" + parentMenuID + "' AND METHOD IS NULL ORDER BY ORDER_INDEX";
            List<SecMenuEntity> list = DataAccess.Select<SecMenuEntity>(sql);
            return list;
        }

        public static List<SecMenuEntity> GetMenusByParentID(string parentMenuID, string personId)
        {
            string sql = @"SELECT SM.* FROM SEC_MENU SM
                            WHERE  SM.METHOD IS NULL AND SM.MENU_ID IN (
                            SELECT SM.MENU_ID
                            FROM SEC_MENU SM
                            INNER JOIN SEC_ROLE_MENU RM ON RM.MENU_ID = SM.MENU_ID
                            INNER JOIN SEC_ROLE R ON R.ROLE_ID = RM.ROLE_ID
                            INNER JOIN SEC_USER_ROLE UR ON UR.ROLE_ID = R.ROLE_ID
							INNER JOIN SEC_USER U ON U.USER_ID = UR.USER_ID
                            WHERE U.PERSON_ID = '" + personId + @"'
                            UNION ALL 
                            SELECT SM.MENU_ID
                            FROM SEC_MENU SM
                            INNER JOIN SEC_ROLE_MENU RM ON RM.MENU_ID = SM.MENU_ID
                            INNER JOIN SEC_ROLE R ON R.ROLE_ID = RM.ROLE_ID
								AND R.POSITION_ID IN (SELECT P.POSITION_ID FROM MST_POSITION P 
                                INNER JOIN HR_PERSON_POSITION PP ON P.POSITION_ID = PP.POSITION_ID
								WHERE PP.PERSON_ID = '" + personId + @"' ))";
            if (string.IsNullOrEmpty(parentMenuID))
            {
                sql += " AND  SM.PARENT_MENU IS NULL";
            }
            else
            {
                sql += " AND  SM.PARENT_MENU = '" + parentMenuID + "'";
            }
            sql += " ORDER BY ORDER_INDEX ";
            List<SecMenuEntity> list = DataAccess.Select<SecMenuEntity>(sql);
            return list;
        }

        public static List<SecMenuEntity> GetMenusNotFormByParentID(string parentMenuID)
        {
            if (string.IsNullOrEmpty(parentMenuID))
            {
                return GetRootMenus();
            }
            string sql = "SELECT * FROM SEC_MENU WHERE PARENT_MENU = '" + parentMenuID + "' AND MENU_TYPE_CODE != '20' AND METHOD IS NULL ORDER BY ORDER_INDEX";
            List<SecMenuEntity> list = DataAccess.Select<SecMenuEntity>(sql);
            return list;
        }

        public static DataSet GetDSMenusByParentID(string parentMenuID)
        {
            string sql = @"SELECT M.*, P.MENU_NAME AS PARENT_MENU_NAME, T.MENU_TYPE_NAME
                            FROM SEC_MENU M
                            LEFT JOIN SEC_MENU P ON
	                            P.MENU_ID = M.PARENT_MENU
                            LEFT JOIN SEC_MENU_TYPE T ON
	                            T.MENU_TYPE_CODE = M.MENU_TYPE_CODE WHERE M.METHOD IS NULL ";
            if (string.IsNullOrEmpty(parentMenuID) == false)
            {
                sql += " AND M.PARENT_MENU = '" + parentMenuID + "'";
            }
            sql += " ORDER BY P.MENU_NAME, M.ORDER_INDEX";
            DataSet ds = DataAccess.SelectDataSet(sql);
            return ds;
        }

        public static List<SecMenuEntity> GetMenusForNotForm()
        {
            string sql = @"SELECT SEC_MENU.*
                            FROM SEC_MENU
                            INNER JOIN SEC_MENU_TYPE ON
	                            SEC_MENU_TYPE.MENU_TYPE_CODE = SEC_MENU.MENU_TYPE_CODE
                            WHERE SEC_MENU_TYPE.MENU_TYPE_NAME != 'FORM' AND METHOD IS NULL ";
            List<SecMenuEntity> list = DataAccess.Select<SecMenuEntity>(sql);
            return list;
        }

        public static List<SecMenuEntity> GetRootMenus()
        {
            string sql = "SELECT * FROM  SEC_MENU WHERE PARENT_MENU IS NULL AND METHOD IS NULL ORDER BY ORDER_INDEX";
            List<SecMenuEntity> list = DataAccess.Select<SecMenuEntity>(sql);
            return list;
        }

        public static SecMenuEntity GetMenuByID(string menuID)
        {
            if (string.IsNullOrEmpty(menuID))
            {
                return null;
            }
            SecMenuEntity module = new SecMenuEntity();
            module.MenuId = menuID;
            return DataAccess.SelectSingle<SecMenuEntity>(module);
        }

        public static SecMenuEntity GetMenuByFormName(string formName)
        {
            if (string.IsNullOrEmpty(formName))
            {
                return null;
            }
            SecMenuEntity module = new SecMenuEntity();
            module.FormName = formName;
            return DataAccess.SelectSingle<SecMenuEntity>(module);
        }

        public static bool UpdateMenu(SecMenuEntity entity)
        {
            return DataAccess.Update(entity);
        }

        public static void UpdateMenu(SecMenuEntity entity, DataAccessBroker broker)
        {
            DataAccess.Update(entity, broker);
        }

        public static bool UpdateMenus(List<SecMenuEntity> list)
        {
            return DataAccess.Update<SecMenuEntity>(list);
        }

        public static void UpdateMenus(List<SecMenuEntity> list, DataAccessBroker broker)
        {
            DataAccess.Update(list, broker);
        }

        public static bool InsertMenu(SecMenuEntity entity)
        {
            return DataAccess.Insert(entity);
        }

        public static void InsertMenu(SecMenuEntity entity,DataAccessBroker broker)
        {
            DataAccess.Insert(entity, broker);
        }

        public static bool InsertMenus(List<SecMenuEntity> list)
        {
            return DataAccess.Insert<SecMenuEntity>(list);
        }

        public static void InsertMenus(List<SecMenuEntity> list, DataAccessBroker broker)
        {
            DataAccess.Insert<SecMenuEntity>(list,broker);
        }

        public static bool DeleteMenu(SecMenuEntity entity)
        {
            return DataAccess.Delete(entity);
        }

        public static bool DeleteMenus(List<SecMenuEntity> list)
        {
            return DataAccess.Delete<SecMenuEntity>(list);
        }
    }
}
